package net.lab1024.sa.admin.module.system.user.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.extra.servlet.ServletUtil;
import io.swagger.v3.oas.annotations.Operation;
import net.lab1024.sa.admin.module.system.sms.SmsCodeService;
import net.lab1024.sa.admin.module.system.type.domain.TypeEntity;
import net.lab1024.sa.admin.module.system.type.domain.TypeVO;
import net.lab1024.sa.admin.module.system.type.service.TypeService;
import net.lab1024.sa.admin.module.system.user.domain.*;
import net.lab1024.sa.admin.module.system.user.domain.form.UserQueryForm;
import net.lab1024.sa.admin.module.system.user.domain.form.UserUpdateForm;
import net.lab1024.sa.admin.module.system.user.service.UserLoginService;
import net.lab1024.sa.admin.module.system.user.service.UserService;
import net.lab1024.sa.admin.util.MobileRequestUtil;
import net.lab1024.sa.base.common.annoation.NoNeedLogin;
import net.lab1024.sa.base.common.constant.RequestHeaderConst;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.module.support.captcha.domain.CaptchaVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 员工登录
 *
 * @Author cp
 * @Date 2021-12-15 21:05:46
 
 
  * @Copyright  cpedu
 */
@RestController
public class UserLoginApi {
    @Resource
    private UserService userService;
    @Resource
    private UserLoginService userloginService;
    @Resource
    private SmsCodeService smsCodeService; // 业务逻辑层
    @Resource
    private TypeService typeService;
    @NoNeedLogin
    @PostMapping("/user/login")
    @Operation(summary = "登录  ")
    public ResponseDTO<UserLoginResultVO> login(@Valid @RequestBody UserLoginForm loginForm, HttpServletRequest request) {
        String ip = ServletUtil.getClientIP(request);
        String userAgent = ServletUtil.getHeaderIgnoreCase(request, RequestHeaderConst.USER_AGENT);
        return userloginService.login(loginForm, ip, userAgent);
    }

    @GetMapping("/user/getLoginInfo")
    @Operation(summary = "获取登录结果信息   ")
    public ResponseDTO<UserLoginResultVO> getLoginInfo() {
        RequestUser a = MobileRequestUtil.getRequestUser();
        UserLoginResultVO loginResult = userloginService.getLoginResult(a);
        String tokenValue = StpUtil.getTokenValue();
        loginResult.setToken(tokenValue);
        return ResponseDTO.ok(loginResult);
    }

    @Operation(summary = "退出登陆")
    @GetMapping("/user/logout")
    public ResponseDTO<String> logout(@RequestHeader(value = RequestHeaderConst.TOKEN, required = false) String token) {
        return userloginService.logout(token, MobileRequestUtil.getRequestUser());
    }

    @Operation(summary = "获取验证码   ")
    @PostMapping("/user/getCaptcha")
    @NoNeedLogin
    public ResponseDTO<CaptchaVO> getCaptcha(@Valid@RequestBody String phone) {
        // 1. 校验手机号格式
        if (!PhoneUtil.isMobile(phone)) {
            return ResponseDTO.userErrorParam("手机号格式不正确！");
        }
//        // 2. 校验发送频率（防止短信轰炸）
//        if (smsCodeService.checkSendFrequency(mobile)) {
//            return ResponseDTO.userErrorParam("发送过于频繁，请稍后再试");
//        }
        // 3. 生成验证码
        String code = smsCodeService.generateCode();
        // 4. 存储验证码到Redis
        smsCodeService.saveCodeToRedis(phone, code);
//        // 5. 调用短信代理发送（通过工厂获取具体实现）
//        SmsSender smsSender = smsCodeService.getSmsSender(); // 可配置默认服务商
//        boolean sendResult = smsSender.send(mobile, code);
        if (false) {
            return ResponseDTO.userErrorParam("短信发送失败，请稍后再试");
        }
        return ResponseDTO.okMsg("验证码已发送");
//        return userloginService.getCaptcha();
    }

    @Operation(summary = "查询用户信息  ")
    @GetMapping("/user/queryById/{userId}")
//    @SaCheckPermission("support:user:query")
    public ResponseDTO<UserVO> queryById(@PathVariable Integer userId) {
        return userService.queryById(userId);
    }
    @Operation(summary = "删除用户信息  ")
    @PostMapping("/user/delete")
    @SaCheckPermission("support:user:delete")
    public ResponseDTO<String> deleteSp(@RequestBody @Valid UserUpdateForm updateForm) {
        return userService.deleteUser(updateForm);
    }
    @Operation(summary = "修改用户信息  ")

    @PostMapping("/user/update")
    @SaCheckPermission("support:user:update")
    public ResponseDTO<String> updateSp(@RequestBody @Valid UserUpdateForm updateForm) {
        return userService.updateUser(updateForm);
    }
    @Operation(summary = "获取专业列表")
    @GetMapping("/user/queryType")
    @SaCheckPermission("support:user:query")
    public ResponseDTO<List<TypeVO>> queryShopPage(@RequestParam(defaultValue = "pro_major") String key) {
        return typeService.getTypeVO(key);
    }
    @Operation(summary = "分页用户信息")
    @PostMapping("/user/query")
    @SaCheckPermission("support:user:query")
    public ResponseDTO<PageResult<UserVO>> queryShopPage(@RequestBody @Valid UserQueryForm queryForm) {
        ResponseDTO<PageResult<UserVO>> pageResult = userService.queryUser(queryForm);
        return userService.queryUser(queryForm);
    }
    @Operation(summary = "分页接单员信息")
    @PostMapping("/user/queryJdy")
    @SaCheckPermission("support:user:query")
    public ResponseDTO<PageResult<JdyVO>> queryJdyPage(@RequestBody @Valid UserQueryForm queryForm) {
        ResponseDTO<PageResult<JdyVO>> pageResult = userService.queryjdy(queryForm);
        return pageResult;
    }
}
